package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.McgsJjfzBo;
import com.ruoyi.tb.lmt.domain.vo.McgsJjfzVo;
import com.ruoyi.tb.lmt.domain.McgsJjfz;
import com.ruoyi.tb.lmt.mapper.McgsJjfzMapper;
import com.ruoyi.tb.lmt.service.IMcgsJjfzService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 制动阀试验台_紧急阀组_MCGSService业务层处理
 *
 * @author ruoyi
 * @date 2023-12-13
 */
@RequiredArgsConstructor
@Service
public class McgsJjfzServiceImpl implements IMcgsJjfzService {

    private final McgsJjfzMapper baseMapper;

    /**
     * 查询制动阀试验台_紧急阀组_MCGS
     */
    @Override
    public McgsJjfzVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询制动阀试验台_紧急阀组_MCGS列表
     */
    @Override
    public TableDataInfo<McgsJjfzVo> queryPageList(McgsJjfzBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<McgsJjfz> lqw = buildQueryWrapper(bo);
        Page<McgsJjfzVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询制动阀试验台_紧急阀组_MCGS列表
     */
    @Override
    public List<McgsJjfzVo> queryList(McgsJjfzBo bo) {
        LambdaQueryWrapper<McgsJjfz> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<McgsJjfz> buildQueryWrapper(McgsJjfzBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<McgsJjfz> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, McgsJjfz::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getDjNo()), McgsJjfz::getDjNo, bo.getDjNo());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsTime()), McgsJjfz::getMcgsTime, bo.getMcgsTime());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsTimeMs()), McgsJjfz::getMcgsTimeMs, bo.getMcgsTimeMs());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsCzz()), McgsJjfz::getMcgsCzz, bo.getMcgsCzz());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJxbh()), McgsJjfz::getMcgsJxbh, bo.getMcgsJxbh());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsFh()), McgsJjfz::getMcgsFh, bo.getMcgsFh());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsSch()), McgsJjfz::getMcgsSch, bo.getMcgsSch());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf1()), McgsJjfz::getMcgsJjf1, bo.getMcgsJjf1());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf2()), McgsJjfz::getMcgsJjf2, bo.getMcgsJjf2());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf3()), McgsJjfz::getMcgsJjf3, bo.getMcgsJjf3());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf4()), McgsJjfz::getMcgsJjf4, bo.getMcgsJjf4());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf5()), McgsJjfz::getMcgsJjf5, bo.getMcgsJjf5());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf6()), McgsJjfz::getMcgsJjf6, bo.getMcgsJjf6());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf7()), McgsJjfz::getMcgsJjf7, bo.getMcgsJjf7());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf8()), McgsJjfz::getMcgsJjf8, bo.getMcgsJjf8());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf9()), McgsJjfz::getMcgsJjf9, bo.getMcgsJjf9());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJjf10()), McgsJjfz::getMcgsJjf10, bo.getMcgsJjf10());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsBy1()), McgsJjfz::getMcgsBy1, bo.getMcgsBy1());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsZsch()), McgsJjfz::getMcgsZsch, bo.getMcgsZsch());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), McgsJjfz::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), McgsJjfz::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, McgsJjfz::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), McgsJjfz::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, McgsJjfz::getUpdateTime, bo.getUpdateTime());
        return lqw;
    }

    /**
     * 新增制动阀试验台_紧急阀组_MCGS
     */
    @Override
    public Boolean insertByBo(McgsJjfzBo bo) {
        McgsJjfz add = BeanUtil.toBean(bo, McgsJjfz.class);
        add.setDelFlag("0");
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改制动阀试验台_紧急阀组_MCGS
     */
    @Override
    public Boolean updateByBo(McgsJjfzBo bo) {
        McgsJjfz update = BeanUtil.toBean(bo, McgsJjfz.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(McgsJjfz entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除制动阀试验台_紧急阀组_MCGS
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public String getMaxMcgsTime(){
        return baseMapper.getMaxMcgsTime();
    }
}
